package com.hh.generator.core.generator;

import com.hh.generator.common.dto.ColumnEntity;
import com.hh.generator.common.dto.TableEntity;
import com.hh.generator.core.Generator;
import com.hh.generator.core.model.ClassModel;
import com.hh.generator.core.model.Config;
import com.hh.generator.core.model.EnumModel;

import java.util.ArrayList;
import java.util.List;

import static com.hh.generator.common.util.TableToClassConvertUtil.enumClassName;

public class EnumGenerator extends AbstractTableBaseGenerator implements Generator {

    private String templateName = "Enum.java.vm";

    public EnumGenerator(Config config, TableEntity tableEntity, List<ColumnEntity> columnEntityList) {
        super(config, tableEntity, columnEntityList);
    }

    @Override
    public String fileName() {
        return null;
    }

    @Override
    public ClassModel buildModel() {
        return null;
    }

    @Override
    protected String enumFileName(ColumnEntity columnEntity) {
        return enumClassName(config.getTablePrefix(), tableEntity.getTableName(), columnEntity.getColumnName()) + ".java";
    }

    @Override
    public ClassModel buildEnumModel(ColumnEntity columnEntity) {
        List<EnumModel.EnumField> enumFields = new ArrayList<>();
        // eg.  枚举 用户状态: 0-启用(enable) | 1-禁用(disable)
        String[] first = columnEntity.getColumnComment().split(":");
        String[] entities = first[1].trim().split("\\|");
        for (String entity : entities) {
            String[] arr = entity.trim().split("-");
            String desc = arr[1].trim();
            String[] descs = desc.split("\\(");
            String cname = descs[0].trim();
            String ename = descs[1].trim().replace(")", "").trim().toLowerCase();
            String eName = ename.toUpperCase();
            EnumModel.EnumField enumField = new EnumModel.EnumField();
            enumField.setName(cname);
            enumField.setConstantName(eName);
            enumField.setIntValue(Integer.valueOf(arr[0].trim()));
            enumFields.add(enumField);
        }
        List<String> imports = new ArrayList<>();
        imports.add(config.getColumnEnumClassName());
        EnumModel model = new EnumModel();
        model.setEnumFields(enumFields);
        model.setPackageName(config.getEnumPackage().replace("xx", config.getModule()));
        model.setImports(imports);
        model.setComment(tableEntity.getTableComment() + " " + columnEntity.getColumnComment());
        model.setAuthor(config.getClassAuthor());
        model.setDate(config.getDate());
        model.setClassName(enumClassName(config.getTablePrefix(), tableEntity.getTableName(), columnEntity.getColumnName()));
        return model;
    }

    @Override
    public String getTemplateName() {
        return templateName;
    }

}
